const express = require('express')
const router =express.Router()

const CommentModel = require('../models/comment')
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/Blog')

router.get('/find',(req,res,next)=>{
    const {blog_id} = req.query
    if(blog_id!=undefined){
      CommentModel.find({blog_id}).then(result=>{
        res.json({
            code:200,
            data:result,
            message:'blog_id查询comment成功',
        })
    })}
    else{
      res.json({
          code:200,
          message:'请输入blog_id',
      })
    }
})

router.post('/create',(req,res,next)=>{
    const {blog_id,content,user_id,nickname,user_photo} = req.body
    console.log(blog_id,content,user_id)
    CommentModel.find().then(result=>{
        const comment = new CommentModel({
          comment_id:result.length,
          blog_id,
          content,
          user_id,
          nickname,
          user_photo,
          time:new Date()
        })
        comment.save().then(result => {
            res.json({
                code:200,
                data:{
                    result
                },
                message:'comment创建完成',
            })
        })
    })
})
module.exports = router